package com.bootdo.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.wx.dao.GoodsClassDao;
import com.bootdo.wx.dao.GoodsGoodsDao;
import com.bootdo.wx.domain.GoodsClassDO;
import com.bootdo.wx.service.GoodsClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class GoodsClassServiceImpl implements GoodsClassService {
	@Autowired
	private GoodsClassDao goodsClassDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	
	@Override
	public GoodsClassDO get(String id){
		return goodsClassDao.get(id);
	}
	
	@Override
	public List<GoodsClassDO> list(Map<String, Object> map){
		return goodsClassDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsClassDao.count(map);
	}
	
	@Override
	public int save(GoodsClassDO goodsClass){
		return goodsClassDao.save(goodsClass);
	}
	
	@Override
	public int update(GoodsClassDO goodsClass){
		return goodsClassDao.update(goodsClass);
	}
	
	@Override
	public int remove(String id){
		return goodsClassDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsClassDao.batchRemove(ids);
	}
	/**
	 * 获取一级分类
	 */
	@Override
	public R classList(Map<String, Object> map) {
		List<GoodsClassDO> goodsClass = goodsClassDao.classList(map);
		return R.ok().put("data", goodsClass);
	}

	@Override
	public R queryByPrentId(Map<String, Object> params) {
		//List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		params.put("level","1");
		params.put("delFlag", "0");
		List<GoodsClassDO> goodsOneClassList = goodsClassDao.queryByPrentId(params);
	/*	for (GoodsClassDO goodsClassDO : goodsOneClassList) {
			Map<String,Object> threeMap = new HashMap<>();
			String twoId = goodsClassDO.getId();
			String name = goodsClassDO.getName();
			params.put("parentId",twoId);
			List<GoodsClassDO> goodsTwoClassList = goodsClassDao.queryByPrentId(params);
			threeMap.put("twoClassName",name);
			threeMap.put("threeClass",goodsTwoClassList);
			list.add(threeMap);
		}*/
		return R.ok().put("data", goodsOneClassList);
	}

	@Override
	public List<GoodsClassDO> queryAll() {
		return goodsClassDao.queryAll();
	}

	@Override
	public Tree<GoodsClassDO> getTree() {
		List<Tree<GoodsClassDO>> trees = new ArrayList<Tree<GoodsClassDO>>();
		List<GoodsClassDO> classDOs = goodsClassDao.list(new HashMap<>(16));
		for (GoodsClassDO goodsClassDO : classDOs) {
			Tree<GoodsClassDO> tree = new Tree<GoodsClassDO>();
			tree.setId(goodsClassDO.getId());
			tree.setParentId(goodsClassDO.getParentId());
			tree.setText(goodsClassDO.getName());
			trees.add(tree);
		}
		// 默认顶级大类为０，根据数据库实际情况调整
		Tree<GoodsClassDO> t = BuildTree.build(trees);
		return t;
	}

    @Override
    public R getSmallClassAndGoods(Map<String, Object> params) {

		if(MapUtil.getStr(params,"classId").equals("-1")){
			params.put("classId","");
			PageHelperUtils.page(params);
			List<Map<String, Object>> maps = goodsGoodsDao.goodsListByClass(params);
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(maps);
			return R.ok().put("data",pageInfo);
		}

		List<Map<String,Object>> goodsClassList = goodsClassDao.queryByBigClassId(params);
		if(goodsClassList.size()>0){
			for (Map<String, Object> stringObjectMap : goodsClassList) {
				HashMap<String, Object> objectObjectHashMap = MapUtil.newHashMap();
				objectObjectHashMap.putAll(stringObjectMap);
				objectObjectHashMap.put("name",MapUtil.getStr(params,"name"));
				List<Map<String, Object>> maps = goodsGoodsDao.goodsListByClass(objectObjectHashMap);
				stringObjectMap.put("goodsList",maps);

			}
		}
		return R.ok().put("data",goodsClassList);

    }

}
